<template>
	<view>
		<textarea v-model="post.content" placeholder="请输入帖子内容"></textarea>
		<button @click="chooseImage">选择图片</button>
		<view v-for="(image, index) in images" :key="index">
			<image :src="image" mode="aspectFit" />
		</view>
		<button @click="publishPost">发布帖子</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				post: {
					userId: 1, // 假设用户ID为1，实际应用中应从登录信息中获取
					content: '',
					createdAt: new Date().toISOString(),
				},
				images: []
			};
		},
		methods: {
			chooseImage() {
				uni.chooseImage({
					count: 5,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						this.images = res.tempFilePaths;
					}
				});
			},
			publishPost() {
				const requestData = {
					...this.post,
					images: this.images.map(image => ({
						imageUrl: image
					})) // 将图片URL转换为对象数组
				};
				uni.request({
					url: 'http://localhost:8083/posts/publish', // 后端接口地址
					method: 'POST',
					data: requestData, // 确保这里发送的是包含images的对象
					success: (res) => {
						uni.showToast({
							title: '发布成功'
						});
						// if (res.data.success) {

						// } else {
						// 	uni.showToast({
						// 		title: '发布失败'
						// 	});
						// }
					},
					fail: () => {
						uni.showToast({
							title: '网络错误',
							icon: 'none'
						});
					}
				});
			}
		}
	};
</script>